<?php
namespace App\Models;

class SmsMtTempQueue extends Base {

    public function initialize() {
        parent::initialize();
    }

    public function getSource() {
        return "sms_mt_temp_queue";
    }

    public function add($mobile) {
        try {
            $query = "INSERT INTO `sms_mt_temp_queue`(`mobile`, `receive_at`)VALUES(:mobile, NOW())";
            if($this->db()->execute($query, ['mobile' => $mobile])) {
                return true;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function gets($mobile, $limit = 5) {
        $limit = is_integer($limit) ? $limit : 5;
        try {
            $query = "SELECT * FROM `sms_mt_temp_queue` WHERE `mobile`=:mobile ORDER BY `receive_at` DESC LIMIT :limit";
            return $this->db()->query($query, [
                'mobile' => $mobile,
                'limit'  => $limit
            ])->fetchAll();
        } catch(\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }
}
